package jnpf.service.impl;

import jnpf.entity.ZXGL_RenWuFanKuiXieBanEntity;
import jnpf.mapper.ZXGL_RenWuFanKuiXieBanMapper;
import jnpf.service.ZXGL_RenWuFanKuiXieBanService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jnpf.util.RandomUtil;

import java.math.BigDecimal;


import jnpf.model.zxgl_renwufankuixieban.ZXGL_RenWuFanKuiXieBanPagination;

import java.lang.reflect.Field;

import com.baomidou.mybatisplus.annotation.TableField;

import java.util.stream.Collectors;

import jnpf.entity.ZXGL_RenWuFanKuiJiaoFuWuEntity;
import jnpf.service.ZXGL_RenWuFanKuiJiaoFuWuService;


import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import java.text.SimpleDateFormat;

import jnpf.util.*;

import java.util.*;

/**
 * ZXGL_RenWuFanKuiXieBan
 * 版本： V2.0
 * 版权： 河北交投智能交通技术有限责任公司
 * 作者： 数字交投项目组
 * 日期： 2025-03-20
 */
@Service
public class ZXGL_RenWuFanKuiXieBanServiceImpl extends ServiceImpl<ZXGL_RenWuFanKuiXieBanMapper, ZXGL_RenWuFanKuiXieBanEntity> implements ZXGL_RenWuFanKuiXieBanService {


    @Autowired
    private UserProvider userProvider;


    @Autowired
    private ZXGL_RenWuFanKuiJiaoFuWuService zXGL_RenWuFanKuiJiaoFuWuService;


    @Override
    public List<ZXGL_RenWuFanKuiXieBanEntity> getList(ZXGL_RenWuFanKuiXieBanPagination zXGL_RenWuFanKuiXieBanPagination) {

        String userId = userProvider.get().getUserId();
        List<String> AllIdList = new ArrayList();
        int total = 0;
        QueryWrapper<ZXGL_RenWuFanKuiXieBanEntity> zXGL_RenWuFanKuiXieBanQueryWrapper = new QueryWrapper<>();
        QueryWrapper<ZXGL_RenWuFanKuiJiaoFuWuEntity> zXGL_RenWuFanKuiJiaoFuWuQueryWrapper = new QueryWrapper<>();
        int zXGL_RenWuFanKuiXieBanNum = 0;
        if (StringUtil.isNotEmpty(zXGL_RenWuFanKuiXieBanPagination.getFkr())) {
            zXGL_RenWuFanKuiXieBanNum++;
            zXGL_RenWuFanKuiXieBanQueryWrapper.lambda().eq(ZXGL_RenWuFanKuiXieBanEntity::getFkr, zXGL_RenWuFanKuiXieBanPagination.getFkr());
        }

        if (StringUtil.isNotEmpty(zXGL_RenWuFanKuiXieBanPagination.getFkbm())) {
            zXGL_RenWuFanKuiXieBanNum++;
            zXGL_RenWuFanKuiXieBanQueryWrapper.lambda().eq(ZXGL_RenWuFanKuiXieBanEntity::getFkbm, zXGL_RenWuFanKuiXieBanPagination.getFkbm());
        }

        if (StringUtil.isNotEmpty(zXGL_RenWuFanKuiXieBanPagination.getFkrq())) {
            zXGL_RenWuFanKuiXieBanNum++;
            List<String> FkrqList = zXGL_RenWuFanKuiXieBanPagination.getFkrq();
            Long fir = Long.valueOf(FkrqList.get(0));
            Long sec = Long.valueOf(FkrqList.get(1));

            zXGL_RenWuFanKuiXieBanQueryWrapper.lambda().ge(ZXGL_RenWuFanKuiXieBanEntity::getFkrq, new Date(fir))
                    .le(ZXGL_RenWuFanKuiXieBanEntity::getFkrq, DateUtil.stringToDate(DateUtil.daFormatYmd(sec) + " 23:59:59"));

        }

        if (StringUtil.isNotEmpty(zXGL_RenWuFanKuiXieBanPagination.getFkxm())) {
            zXGL_RenWuFanKuiXieBanNum++;
            zXGL_RenWuFanKuiXieBanQueryWrapper.lambda().like(ZXGL_RenWuFanKuiXieBanEntity::getFkxm, zXGL_RenWuFanKuiXieBanPagination.getFkxm());
        }

        if (AllIdList.size() > 0) {
            zXGL_RenWuFanKuiXieBanQueryWrapper.lambda().in(ZXGL_RenWuFanKuiXieBanEntity::getId, AllIdList);
        }
        //排序
        if (StringUtil.isEmpty(zXGL_RenWuFanKuiXieBanPagination.getSidx())) {
            zXGL_RenWuFanKuiXieBanQueryWrapper.lambda().orderByDesc(ZXGL_RenWuFanKuiXieBanEntity::getFkr);
        } else {
            try {
                String sidx = zXGL_RenWuFanKuiXieBanPagination.getSidx();
                ZXGL_RenWuFanKuiXieBanEntity zXGL_RenWuFanKuiXieBanEntity = new ZXGL_RenWuFanKuiXieBanEntity();
                Field declaredField = zXGL_RenWuFanKuiXieBanEntity.getClass().getDeclaredField(sidx);
                declaredField.setAccessible(true);
                String value = declaredField.getAnnotation(TableField.class).value();
                zXGL_RenWuFanKuiXieBanQueryWrapper = "asc".equals(zXGL_RenWuFanKuiXieBanPagination.getSort().toLowerCase()) ? zXGL_RenWuFanKuiXieBanQueryWrapper.orderByAsc(value) : zXGL_RenWuFanKuiXieBanQueryWrapper.orderByDesc(value);
            } catch (NoSuchFieldException e) {
                e.printStackTrace();
            }
        }
        if ((total > 0 && AllIdList.size() > 0) || total == 0) {
            Page<ZXGL_RenWuFanKuiXieBanEntity> page = new Page<>(zXGL_RenWuFanKuiXieBanPagination.getCurrentPage(), zXGL_RenWuFanKuiXieBanPagination.getPageSize());
            IPage<ZXGL_RenWuFanKuiXieBanEntity> userIPage = this.page(page, zXGL_RenWuFanKuiXieBanQueryWrapper);
            return zXGL_RenWuFanKuiXieBanPagination.setData(userIPage.getRecords(), userIPage.getTotal());
        } else {
            List<ZXGL_RenWuFanKuiXieBanEntity> list = new ArrayList();
            return zXGL_RenWuFanKuiXieBanPagination.setData(list, list.size());
        }
    }

    @Override
    public List<ZXGL_RenWuFanKuiXieBanEntity> getTypeList(ZXGL_RenWuFanKuiXieBanPagination zXGL_RenWuFanKuiXieBanPagination, String dataType) {
        String userId = userProvider.get().getUserId();
        List<String> AllIdList = new ArrayList();
        int total = 0;
        QueryWrapper<ZXGL_RenWuFanKuiXieBanEntity> zXGL_RenWuFanKuiXieBanQueryWrapper = new QueryWrapper<>();
        QueryWrapper<ZXGL_RenWuFanKuiJiaoFuWuEntity> zXGL_RenWuFanKuiJiaoFuWuQueryWrapper = new QueryWrapper<>();
        int zXGL_RenWuFanKuiXieBanNum = 0;
        if (StringUtil.isNotEmpty(zXGL_RenWuFanKuiXieBanPagination.getFkr())) {
            zXGL_RenWuFanKuiXieBanNum++;
            zXGL_RenWuFanKuiXieBanQueryWrapper.lambda().eq(ZXGL_RenWuFanKuiXieBanEntity::getFkr, zXGL_RenWuFanKuiXieBanPagination.getFkr());
        }

        if (StringUtil.isNotEmpty(zXGL_RenWuFanKuiXieBanPagination.getFkbm())) {
            zXGL_RenWuFanKuiXieBanNum++;
            zXGL_RenWuFanKuiXieBanQueryWrapper.lambda().eq(ZXGL_RenWuFanKuiXieBanEntity::getFkbm, zXGL_RenWuFanKuiXieBanPagination.getFkbm());
        }

        if (StringUtil.isNotEmpty(zXGL_RenWuFanKuiXieBanPagination.getFkrq())) {
            zXGL_RenWuFanKuiXieBanNum++;
            List<String> FkrqList = zXGL_RenWuFanKuiXieBanPagination.getFkrq();
            Long fir = Long.valueOf(FkrqList.get(0));
            Long sec = Long.valueOf(FkrqList.get(1));

            zXGL_RenWuFanKuiXieBanQueryWrapper.lambda().ge(ZXGL_RenWuFanKuiXieBanEntity::getFkrq, new Date(fir))
                    .le(ZXGL_RenWuFanKuiXieBanEntity::getFkrq, DateUtil.stringToDate(DateUtil.daFormatYmd(sec) + " 23:59:59"));

        }

        if (StringUtil.isNotEmpty(zXGL_RenWuFanKuiXieBanPagination.getFkxm())) {
            zXGL_RenWuFanKuiXieBanNum++;
            zXGL_RenWuFanKuiXieBanQueryWrapper.lambda().like(ZXGL_RenWuFanKuiXieBanEntity::getFkxm, zXGL_RenWuFanKuiXieBanPagination.getFkxm());
        }

        if (AllIdList.size() > 0) {
            zXGL_RenWuFanKuiXieBanQueryWrapper.lambda().in(ZXGL_RenWuFanKuiXieBanEntity::getId, AllIdList);
        }
        //排序
        if (StringUtil.isEmpty(zXGL_RenWuFanKuiXieBanPagination.getSidx())) {
            zXGL_RenWuFanKuiXieBanQueryWrapper.lambda().orderByDesc(ZXGL_RenWuFanKuiXieBanEntity::getFkr);
        } else {
            try {
                String sidx = zXGL_RenWuFanKuiXieBanPagination.getSidx();
                ZXGL_RenWuFanKuiXieBanEntity zXGL_RenWuFanKuiXieBanEntity = new ZXGL_RenWuFanKuiXieBanEntity();
                Field declaredField = zXGL_RenWuFanKuiXieBanEntity.getClass().getDeclaredField(sidx);
                declaredField.setAccessible(true);
                String value = declaredField.getAnnotation(TableField.class).value();
                zXGL_RenWuFanKuiXieBanQueryWrapper = "asc".equals(zXGL_RenWuFanKuiXieBanPagination.getSort().toLowerCase()) ? zXGL_RenWuFanKuiXieBanQueryWrapper.orderByAsc(value) : zXGL_RenWuFanKuiXieBanQueryWrapper.orderByDesc(value);
            } catch (NoSuchFieldException e) {
                e.printStackTrace();
            }
        }
        if ("0".equals(dataType)) {
            if ((total > 0 && AllIdList.size() > 0) || total == 0) {
                Page<ZXGL_RenWuFanKuiXieBanEntity> page = new Page<>(zXGL_RenWuFanKuiXieBanPagination.getCurrentPage(), zXGL_RenWuFanKuiXieBanPagination.getPageSize());
                IPage<ZXGL_RenWuFanKuiXieBanEntity> userIPage = this.page(page, zXGL_RenWuFanKuiXieBanQueryWrapper);
                return zXGL_RenWuFanKuiXieBanPagination.setData(userIPage.getRecords(), userIPage.getTotal());
            } else {
                List<ZXGL_RenWuFanKuiXieBanEntity> list = new ArrayList();
                return zXGL_RenWuFanKuiXieBanPagination.setData(list, list.size());
            }
        } else {
            return this.list(zXGL_RenWuFanKuiXieBanQueryWrapper);
        }
    }

    @Override
    public ZXGL_RenWuFanKuiXieBanEntity getInfo(String id) {
        QueryWrapper<ZXGL_RenWuFanKuiXieBanEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(ZXGL_RenWuFanKuiXieBanEntity::getId, id);
        return this.getOne(queryWrapper);
    }

    @Override
    public void create(ZXGL_RenWuFanKuiXieBanEntity entity) {
        this.save(entity);
    }

    @Override
    public boolean update(String id, ZXGL_RenWuFanKuiXieBanEntity entity) {
        entity.setId(id);
        return this.updateById(entity);
    }

    @Override
    public void delete(ZXGL_RenWuFanKuiXieBanEntity entity) {
        if (entity != null) {
            this.removeById(entity.getId());
        }
    }

    //子表方法
    @Override
    public List<ZXGL_RenWuFanKuiJiaoFuWuEntity> GetZXGL_RenWuFanKuiJiaoFuWuList(String id) {
        QueryWrapper<ZXGL_RenWuFanKuiJiaoFuWuEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(ZXGL_RenWuFanKuiJiaoFuWuEntity::getId, id);
        return zXGL_RenWuFanKuiJiaoFuWuService.list(queryWrapper);
    }

    //列表子表数据方法

}